package com.oa.dao.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

import javax.annotation.Resource;

import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessDefinitionQuery;
import org.jbpm.api.ProcessEngine;
import org.springframework.stereotype.Repository;

import com.oa.base.BaseDaoImpl;
import com.oa.dao.LiuChengDingYiDao;
import com.oa.entity.LiuChengDingYi;

/**
 * @author FengMengHuo 流程定义接口的实现
 */
@Repository("liuChengDingYiDao")
public class LiuChengDingYiDaoImpl extends BaseDaoImpl<LiuChengDingYi>
		implements LiuChengDingYiDao {
	@Resource(name = "processEngine")
	private ProcessEngine processEngine;

	public List<ProcessDefinition> findLiuChengDingYiList() {
		List<ProcessDefinition> processDefinitionList = processEngine
				.getRepositoryService().createProcessDefinitionQuery()
				.orderAsc(ProcessDefinitionQuery.PROPERTY_VERSION) // 按版本的升序排列
				.list();
		Map<String, ProcessDefinition> processDefinitionMap = new HashMap<String, ProcessDefinition>();
		for (ProcessDefinition processDefinition : processDefinitionList) {
			// 用processDefinition.getKey()可以替换前一个版本，可以显示最新版本
			processDefinitionMap.put(processDefinition.getKey(),
					processDefinition);
		}
		// 把封装好的定义流程map集合，转成list集合返回
		List<ProcessDefinition> pList = new ArrayList<ProcessDefinition>(
				processDefinitionMap.values());
		return pList;
	}

	// 部署流程定义
	public void buShuLiuChengDingYi(LiuChengDingYi liuChengDingYi) {
		try {
			// 获取页面要上传的 Zip 压缩文件
			File file = liuChengDingYi.getUpload();
			ZipInputStream zipInputStream = new ZipInputStream(
					new FileInputStream(file));
			// 部署流程定义
			processEngine.getRepositoryService().createDeployment()
					.addResourcesFromZipInputStream(zipInputStream).deploy();
			zipInputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//删除流程定义
	public void deleteLiuChengDingYi(LiuChengDingYi liuChengDingYi) {
		// 获取页面传递的流程定义的Key
		String key = liuChengDingYi.getKey();
		//处理前台乱码问题
		/*try {
			key= new String(key.getBytes("iso-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}*/
		// 使用 key 获取所有版本的流程定义
		List<ProcessDefinition> list = processEngine.getRepositoryService()
											.createProcessDefinitionQuery()
											.processDefinitionKey(key)
											.list();
		// 删除流程定义
		for (ProcessDefinition pd:list) {
			String deploymentId = pd.getDeploymentId();
			processEngine.getRepositoryService()
							.deleteDeploymentCascade(deploymentId);
		}
		
		
	}

	// 查看流程定义图
	public InputStream lookImageById(LiuChengDingYi liuChengDingYi) {
		//获取页面传递的流程定义id
				String liuChengDingYiID = liuChengDingYi.getId();
				/*try {
					liuChengDingYiID=new String(liuChengDingYiID.getBytes("iso-8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}*/
				System.out.println("========="+liuChengDingYiID);
				ProcessDefinition pd = processEngine.getRepositoryService()
						               .createProcessDefinitionQuery()
						               .processDefinitionId(liuChengDingYiID)
						               .uniqueResult();
				String deploymentId = pd.getDeploymentId();
				String resourceName = pd.getImageResourceName();
				InputStream inputStream = processEngine.getRepositoryService()
				                     .getResourceAsStream(deploymentId, resourceName);
				return inputStream; 
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
